﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

using CE.Mail;
using CE.Image;
using CE.User;
using CE.Database;
using CE.TravelInsurance;
using CE.Global;
using CE.Provider;

public partial class travel_insurance_redirect : System.Web.UI.Page
{
    private string CTName2 = "";
    private string CTNRIC = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.IsSecureConnection)
            {
                string redirectUrl = Request.Url.ToString().Replace("https:", "http:");
                Response.Redirect(redirectUrl);
            } 

            if (Request.QueryString["ResultId"] != null && Request.QueryString["ProviderId"] != null && Request.QueryString["Premium"] != null && Request.QueryString["TAId"] != null && Request.QueryString["UserId"] != null)
            {
                bool _Mail = false;
                string _strTIMailActive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("IsTIMailActive");
                if (_strTIMailActive == "1")
                {
                    _Mail = true;
                }
                
                Int64 _resultId = Convert.ToInt64(Request.QueryString["ResultId"].ToString());
                Int64 _companyId = Convert.ToInt64(Request.QueryString["ProviderId"].ToString());
                string _AnnualPremium = Request.QueryString["Premium"].ToString();
                Int64 TAId = Convert.ToInt64(Request.QueryString["TAId"].ToString());
                Int64 _UserId = Convert.ToInt64(Request.QueryString["UserId"].ToString());
                
                CTName2 = Request.QueryString["name2"].ToString();
                CTNRIC = Request.QueryString["nric"].ToString();

                Session[CE.Global.General.ProviderCompanyId] = _companyId.ToString();

                string CoverType = "";
                string Trip = "S";
                Int64 CountryId = 0;
                DateTime Startdate = DateTime.Parse("1/1/1900");
                DateTime Enddate = DateTime.Parse("1/1/1900");

                //get current values
                TAApplicantSearchDB _taapplicantsearchdb = new TAApplicantSearchDB();
                TAApplicantSearch _taapplicantsearch = new TAApplicantSearch();



                _taapplicantsearch = _taapplicantsearchdb.GetTAApplicantSearchItem(TAId);

                if (_taapplicantsearch != null)
                {
                    CoverType = _taapplicantsearch.PlanType;
                    CountryId = _taapplicantsearch.CountryID;
                    Trip = _taapplicantsearch.TripType;
                    Startdate = _taapplicantsearch.StartDate;
                    Enddate = _taapplicantsearch.EndDate;

                    LogUserSelectHistory(CoverType, _resultId, _companyId);
                }

                //update total premium field
                TAApplicantSearchDB _taapplicantsearchdbBuy = new TAApplicantSearchDB();
                Int64 UpdatedId = 0;
                UpdatedId = _taapplicantsearchdbBuy.UpdatePremium(TAId, Convert.ToSingle(_AnnualPremium.ToString()), _resultId, _companyId);

                //update profile if not logged in
                if (_UserId == 0 || Convert.ToInt32(Session[CE.Global.General.UserTypeId]) == Convert.ToInt32(CE.Global.CEUserType.Provider))
                {
                    if (Request.QueryString["fullname"] != null && Request.QueryString["mobile"] != null && Request.QueryString["email"] != null)
                    {
                        string fullname = Request.QueryString["fullname"].ToString();
                        string mobile = Request.QueryString["mobile"].ToString();
                        string email = Request.QueryString["email"].ToString();

                        _taapplicantsearchdb.UpdateProfileDetails(TAId, fullname, mobile, email);
                    }
                }

                if (UpdatedId > 0)
                {
                    if (_companyId == 23)
                    {
                        string _PlanName, _PlanId, _PlanType, _PlanTypeId, _CountryCode, _RegionalId;
                        _PlanName = ""; _PlanId = ""; _PlanType = ""; _PlanTypeId = ""; _CountryCode = ""; _RegionalId = "";
                        TAQuoteResultDB _taquoteResultdb = new TAQuoteResultDB();
                        DataSet _dsPlanHSBC = new DataSet();
                        _dsPlanHSBC = _taquoteResultdb.GetTAPlanDetailsHSBC(_companyId, _resultId, CountryId, TAId);
                        if (_dsPlanHSBC.Tables[0].Rows.Count > 0)
                        {
                            _PlanName = _dsPlanHSBC.Tables[0].Rows[0]["PlanName"].ToString();
                            _PlanType = _dsPlanHSBC.Tables[0].Rows[0]["PlanType"].ToString();

                            if (_PlanName == "Classic")
                            {
                                _PlanId = "1";
                            }
                            else if (_PlanName == "Superior")
                            {
                                _PlanId = "2";
                            }

                            if (_PlanType == "Individual")
                            {
                                _PlanTypeId = "1";
                            }
                            else if (_PlanType == "IndividualSpouse")
                            {
                                _PlanTypeId = "2";
                            }
                            else if (_PlanType == "IndividualChild")
                            {
                                _PlanTypeId = "3";
                            }
                            else if (_PlanType == "Family")
                            {
                                _PlanTypeId = "4";
                            }
                        }
                        if (_dsPlanHSBC.Tables.Count > 1)
                        {
                            if (_dsPlanHSBC.Tables[1].Rows.Count > 0)
                            {
                                _CountryCode = _dsPlanHSBC.Tables[1].Rows[0]["CountryCode"].ToString();
                                _RegionalId = _dsPlanHSBC.Tables[1].Rows[0]["RegionalId"].ToString();
                            }
                        }
                        if (_dsPlanHSBC.Tables.Count > 2)
                        {
                            if (_dsPlanHSBC.Tables[2].Rows.Count > 0)
                            {
                                if (Trip != "S")
                                {
                                    _RegionalId = _dsPlanHSBC.Tables[2].Rows[0]["RegionalId"].ToString();
                                }
                            }
                        }

                        if (SaveSelectedInsurance(TAId, _resultId, _companyId, Convert.ToSingle(_AnnualPremium), _UserId, true))
                        {
                            string _url = "";
                            if (Trip == "S")
                            {
                                _url = "https://www.apps.asiapacific.hsbc.com/1/2/sghi/miscellaneous/travel-insurance?";
                                _url += "WABFormEntryCommand=cmd_init&InsuredData.srccode=CXP&quotationdata.affinity_code=CXP&";
                                _url += "quotationdata.trip_type=2&";
                                _url += "quotationdata.start_date=" + Startdate.Day.ToString("00") + "/" + Startdate.Month.ToString("00") + "/" + Startdate.Year + "&";
                                _url += "quotationdata.end_date=" + Enddate.Day.ToString("00") + "/" + Enddate.Month.ToString("00") + "/" + Enddate.Year + "&";
                                _url += "quotationdata.destination_single=" + _CountryCode + "&";
                                _url += "quotationdata.plan_type=" + _PlanTypeId + "&quotationdata.coverage_plan=" + _PlanId;

                                if (_Mail == true)
                                {
                                    if (SaveMailStatus(TAId, _resultId, _companyId, true))
                                    {
                                        ProposalNotification(TAId, _companyId, _UserId);
                                    }
                                }
                                Response.Redirect(_url.ToString());
                            }
                            else
                            {
                                _url = "https://www.apps.asiapacific.hsbc.com/1/2/sghi/miscellaneous/travel-insurance?";
                                _url += "WABFormEntryCommand=cmd_init&InsuredData.srccode=CXP&quotationdata.affinity_code=CXP&";
                                _url += "quotationdata.trip_type=1&";
                                _url += "quotationdata.start_date=" + Startdate.Day.ToString("00") + "/" + Startdate.Month.ToString("00") + "/" + Startdate.Year + "&";
                                _url += "quotationdata.destination_annual=" + _RegionalId + "&";
                                _url += "quotationdata.plan_type=" + _PlanTypeId + "&quotationdata.coverage_plan=" + _PlanId;

                                if (_Mail == true)
                                {
                                    if (SaveMailStatus(TAId, _resultId, _companyId, true))
                                    {
                                        ProposalNotification(TAId, _companyId, _UserId);
                                    }
                                }
                                Response.Redirect(_url.ToString());
                            }
                        }
                    }
                    else if (_companyId == 37)
                    {
                        Int32 _duration = 0;
                        string _destinationCode, _durationRange, _numberOfAdults, _numberOfChildren, _TripType;
                        _destinationCode = ""; _durationRange = ""; _numberOfAdults = ""; _numberOfChildren = ""; _TripType = "";
                        TAQuoteResultDB _taquoteResultdb = new TAQuoteResultDB();
                        DataSet _dsPlan1Cover = new DataSet();
                        _dsPlan1Cover = _taquoteResultdb.GetTAPlanDetails1Cover(_companyId, _resultId, CountryId, TAId);
                        if (_dsPlan1Cover.Tables[0].Rows.Count > 0)
                        {
                            _TripType = _dsPlan1Cover.Tables[0].Rows[0]["TripType"].ToString();
                            if (_TripType == "Single")
                            {
                                if (_dsPlan1Cover.Tables[1].Rows.Count > 0)
                                {
                                    _destinationCode = _dsPlan1Cover.Tables[1].Rows[0]["DestinationCode"].ToString();
                                }
                                _duration = Convert.ToInt32(_dsPlan1Cover.Tables[0].Rows[0]["Duration"].ToString());
                                _numberOfAdults = _dsPlan1Cover.Tables[0].Rows[0]["NoofAdults"].ToString();
                                _numberOfChildren = _dsPlan1Cover.Tables[0].Rows[0]["NoofChilds"].ToString();
                            }
                            else
                            {
                                _destinationCode = _dsPlan1Cover.Tables[0].Rows[0]["DestinationCode"].ToString();
                                _duration = Convert.ToInt32(_dsPlan1Cover.Tables[0].Rows[0]["Duration"].ToString());
                                _numberOfAdults = _dsPlan1Cover.Tables[0].Rows[0]["NoofAdults"].ToString();
                                _numberOfChildren = _dsPlan1Cover.Tables[0].Rows[0]["NoofChilds"].ToString();
                            }
                        }

                        if (SaveSelectedInsurance(TAId, _resultId, _companyId, Convert.ToSingle(_AnnualPremium), _UserId, true))
                        {
                            string _url = "";
                            _durationRange = Get1CoverTravelDuration(_duration);
                            _url = "http://www.1cover.com.sg/travelinsurance/view-quote.html?";
                            _url += "destinationCode=" + _destinationCode + "&durationRange=" + _durationRange + "&";
                            _url += "numberOfAdults=" + _numberOfAdults + "&numberOfChildren=" + _numberOfChildren + "&affID=82";

                            if (_Mail == true)
                            {
                                if (SaveMailStatus(TAId, _resultId, _companyId, true))
                                {
                                    ProposalNotification(TAId, _companyId, _UserId);
                                }
                            }
                            Response.Redirect(_url.ToString());
                        }
                    }
                    else if (_companyId == 38)
                    {
                        string _PolicyType, _Package, _PlanType, _StartDate, _EndDate, _Destination, _Name, _DOB, _Phone, _Email, _TotalTravelers;
                        _PolicyType = ""; _Package = ""; _PlanType = ""; _StartDate = ""; _EndDate = ""; _Destination = ""; _Name = ""; _DOB = "";
                        _Phone = ""; _Email = ""; _TotalTravelers = "";
                        string eAnnualPremium, ePolicyType, ePackage, ePlanType, eStartDate, eEndDate, eDestination, eName, eDOB, ePhone, eEmail, eTotalTravelers;
                        eAnnualPremium = ""; ePolicyType = ""; ePackage = ""; ePlanType = ""; eStartDate = ""; eEndDate = ""; eDestination = "";
                        eName = ""; eDOB = ""; ePhone = ""; eEmail = ""; eTotalTravelers = "";
                        TAQuoteResultDB _taquoteResultdb = new TAQuoteResultDB();
                        DataSet _dsPlanDA = new DataSet();
                        _dsPlanDA = _taquoteResultdb.GetTAPlanDetailsDA(_companyId, _resultId, CountryId, TAId);
                        if (_dsPlanDA.Tables[0].Rows.Count > 0)
                        {

                            _Package = _dsPlanDA.Tables[0].Rows[0]["PlanName"].ToString();
                            _PlanType = _dsPlanDA.Tables[0].Rows[0]["PlanType"].ToString();
                            if (_dsPlanDA.Tables[1].Rows.Count > 0)
                            {
                                _PolicyType = _dsPlanDA.Tables[1].Rows[0]["TripType"].ToString();
                                _StartDate = _dsPlanDA.Tables[1].Rows[0]["StartDate"].ToString();
                                _EndDate = _dsPlanDA.Tables[1].Rows[0]["EndDate"].ToString();
                                if (_PolicyType == "Annual")
                                {
                                    _Destination = _dsPlanDA.Tables[1].Rows[0]["Description"].ToString();
                                }
                                else
                                {
                                    if (_dsPlanDA.Tables[2].Rows.Count > 0)
                                    {
                                        _Destination = _dsPlanDA.Tables[2].Rows[0]["Description"].ToString();
                                    }
                                }
                                _Name = _dsPlanDA.Tables[1].Rows[0]["SurName"].ToString();
                                _DOB = _dsPlanDA.Tables[1].Rows[0]["DOB"].ToString();
                                _Phone = _dsPlanDA.Tables[1].Rows[0]["Mobile"].ToString();
                                _Email = _dsPlanDA.Tables[1].Rows[0]["Email"].ToString();
                                _TotalTravelers = _dsPlanDA.Tables[1].Rows[0]["NoOfTravellers"].ToString();
                            }
                        }
                        string key = "cxDAtravel";
                        CXDecryption.Decryption cxd = new CXDecryption.Decryption();
                        //AnnualPremium
                        if (_AnnualPremium.ToString() != "" && _AnnualPremium.ToString() != null)
                        {
                            byte[] AnnualPremium = cxd.Encrypt(_AnnualPremium, key);
                            eAnnualPremium = Convert.ToBase64String(AnnualPremium);
                        }
                        //Package
                        if (_Package.ToString() != "" && _Package.ToString() != null)
                        {
                            byte[] Package = cxd.Encrypt(_Package, key);
                            ePackage = Convert.ToBase64String(Package);
                        }
                        //PlanType
                        if (_PlanType.ToString() != "" && _PlanType.ToString() != null)
                        {
                            byte[] PlanType = cxd.Encrypt(_PlanType, key);
                            ePlanType = Convert.ToBase64String(PlanType);
                        }
                        //PolicyType
                        if (_PolicyType.ToString() != "" && _PolicyType.ToString() != null)
                        {
                            byte[] PolicyType = cxd.Encrypt(_PolicyType, key);
                            ePolicyType = Convert.ToBase64String(PolicyType);
                        }
                        //StartDate
                        if (_StartDate.ToString() != "" && _StartDate.ToString() != null)
                        {
                            byte[] StartDate = cxd.Encrypt(_StartDate, key);
                            eStartDate = Convert.ToBase64String(StartDate);
                        }
                        //EndDate
                        if (_EndDate.ToString() != "" && _EndDate.ToString() != null)
                        {
                            byte[] EndDate = cxd.Encrypt(_EndDate, key);
                            eEndDate = Convert.ToBase64String(EndDate);
                        }
                        //Destination
                        if (_Destination.ToString() != "" && _Destination.ToString() != null)
                        {
                            byte[] Destination = cxd.Encrypt(_Destination, key);
                            eDestination = Convert.ToBase64String(Destination);
                        }
                        //Name
                        if (_Name.ToString() != "" && _Name.ToString() != null)
                        {
                            byte[] Name = cxd.Encrypt(_Name, key);
                            eName = Convert.ToBase64String(Name);
                        }
                        //DOB
                        if (_DOB.ToString() != "" && _DOB.ToString() != null)
                        {
                            byte[] DOB = cxd.Encrypt(_DOB, key);
                            eDOB = Convert.ToBase64String(DOB);
                        }
                        //Phone
                        if (_Phone.ToString() != "" && _Phone.ToString() != null)
                        {
                            byte[] Phone = cxd.Encrypt(_Phone, key);
                            ePhone = Convert.ToBase64String(Phone);
                        }
                        //Email
                        if (_Email.ToString() != "" && _Email.ToString() != null)
                        {
                            byte[] Email = cxd.Encrypt(_Email, key);
                            eEmail = Convert.ToBase64String(Email);
                        }
                        //TotalTravelers
                        if (_TotalTravelers.ToString() != "" && _TotalTravelers.ToString() != null)
                        {
                            byte[] TotalTravelers = cxd.Encrypt(_TotalTravelers, key);
                            eTotalTravelers = Convert.ToBase64String(TotalTravelers);
                        }

                        if (SaveSelectedInsurance(TAId, _resultId, _companyId, Convert.ToSingle(_AnnualPremium), _UserId, true))
                        {
                            string _url = "";
                            //https://www.directasia.com/Quotation/Travel/QuoteDetails.aspx
                            //http://www.dai.com.sg/quotation/travel/ITravelQuote.aspx
                            //http://www.directasia.com/quotation/travel/ITravelQuote.aspx? (Sakthi on 25 May 2011)
                            _url = CE.Configuration.MyConfigurationManager.GetAppSettingValue("DirectAsiaTIPath");
                            _url += "PolicyType=" + ePolicyType + "&PlanType=" + ePlanType + "&";
                            _url += "Name=" + eName + "&email=" + eEmail + "&Phone=" + ePhone + "&Destination=" + eDestination + "&";
                            _url += "PolicyStartDate=" + eStartDate + "&PolicyEndDate=" + eEndDate + "&DOB=" + eDOB + "&Package=" + ePackage + "&";
                            _url += "Premium=" + eAnnualPremium + "&NumTravellers=" + eTotalTravelers;

                            if (_Mail == true)
                            {
                                if (SaveMailStatus(TAId, _resultId, _companyId, true))
                                {
                                    ProposalNotification(TAId, _companyId, _UserId);
                                }
                            }
                            Response.Redirect(_url.ToString());
                        }
                    }
                    else if (_companyId == 34) //CHINA TAIPING
                    {
                        Int32 _duration = 0;
                        string _TripType, _PlanType, _Name, _Email, _DOBAdult1, _DOBAdult2, _MobileNum, _AreaName, _CountryName;
                        string _StartDate, _EndDate, _Premium, _NoofChilds, _PlanName;

                        _TripType = ""; _PlanType = ""; _Name = ""; _Email = ""; _DOBAdult1 = ""; _DOBAdult2 = ""; _MobileNum = "";
                        _AreaName = ""; _CountryName = ""; _StartDate = ""; _EndDate = ""; _Premium = ""; _NoofChilds = "";
                        _PlanName = "";

                        TAQuoteResultDB _taquoteResultdb = new TAQuoteResultDB();
                        DataSet _dsPlanChinaTaping = new DataSet();
                        _dsPlanChinaTaping = _taquoteResultdb.GetTAPlanDetailsChinaTaiping(_companyId, _resultId, CountryId, TAId);

                        if (_dsPlanChinaTaping.Tables[0].Rows.Count > 0)
                        {
                            _PlanType = _dsPlanChinaTaping.Tables[0].Rows[0]["PlanType"].ToString();
                            _TripType = _dsPlanChinaTaping.Tables[0].Rows[0]["TripType"].ToString();
                            _Name = _dsPlanChinaTaping.Tables[0].Rows[0]["Name"].ToString();
                            _Email = _dsPlanChinaTaping.Tables[0].Rows[0]["EmailAddress"].ToString();
                            _MobileNum = _dsPlanChinaTaping.Tables[0].Rows[0]["MobileNum"].ToString();
                            _DOBAdult1 = _dsPlanChinaTaping.Tables[0].Rows[0]["DOBAdult1"].ToString();
                            _DOBAdult2 = _dsPlanChinaTaping.Tables[0].Rows[0]["DOBAdult2"].ToString();
                            _StartDate = _dsPlanChinaTaping.Tables[0].Rows[0]["StartDate"].ToString();
                            _EndDate = _dsPlanChinaTaping.Tables[0].Rows[0]["EndDate"].ToString();
                            _Premium = _dsPlanChinaTaping.Tables[0].Rows[0]["TotalPremium"].ToString();
                            _NoofChilds = _dsPlanChinaTaping.Tables[0].Rows[0]["NoofChilds"].ToString();

                            if (_TripType == "Single Trip")
                            {
                                _CountryName = _dsPlanChinaTaping.Tables[1].Rows[0]["CountryName"].ToString();
                                _AreaName = _dsPlanChinaTaping.Tables[2].Rows[0]["AreaName"].ToString();
                                _PlanName = _dsPlanChinaTaping.Tables[3].Rows[0]["PlanName"].ToString();
                            }
                            else
                            {
                                _AreaName = _dsPlanChinaTaping.Tables[0].Rows[0]["AreaName"].ToString();
                                _PlanName = _dsPlanChinaTaping.Tables[1].Rows[0]["PlanName"].ToString();
                            }

                        }


                        string key = "cxDAtravel";
                        CXDecryption.Decryption cxd = new CXDecryption.Decryption();

                        if (_TripType.ToString() != "" && _TripType.ToString() != null)
                        {
                            byte[] bTripType = cxd.Encrypt(_TripType, key);
                            _TripType = Convert.ToBase64String(bTripType);
                        }

                        if (_PlanType.ToString() != "" && _PlanType.ToString() != null)
                        {
                            byte[] bPlanType = cxd.Encrypt(_PlanType, key);
                            _PlanType = Convert.ToBase64String(bPlanType);
                        }

                        if (_PlanName.ToString() != "" && _PlanName.ToString() != null)
                        {
                            byte[] bPlanName = cxd.Encrypt(_PlanName, key);
                            _PlanName = Convert.ToBase64String(bPlanName);
                        }

                        if (_Name.ToString() != "" && _Name.ToString() != null)
                        {
                            byte[] bName = cxd.Encrypt(_Name, key);
                            _Name = Convert.ToBase64String(bName);
                        }

                        if (_DOBAdult1.ToString() != "" && _DOBAdult1.ToString() != null)
                        {
                            byte[] bDOBAdult1 = cxd.Encrypt(_DOBAdult1, key);
                            _DOBAdult1 = Convert.ToBase64String(bDOBAdult1);
                        }

                        if (_MobileNum.ToString() != "" && _MobileNum.ToString() != null)
                        {
                            byte[] bMobileNum = cxd.Encrypt(_MobileNum, key);
                            _MobileNum = Convert.ToBase64String(bMobileNum);
                        }

                        if (_Email.ToString() != "" && _Email.ToString() != null)
                        {
                            byte[] bEmail = cxd.Encrypt(_Email, key);
                            _Email = Convert.ToBase64String(bEmail);
                        }

                        if (CTName2.ToString() != "" && CTName2.ToString() != null)
                        {
                            byte[] bCTName2 = cxd.Encrypt(CTName2, key);
                            CTName2 = Convert.ToBase64String(bCTName2);
                        }

                        if (_DOBAdult2.ToString() != "" && _DOBAdult2.ToString() != null)
                        {
                            byte[] bDOBAdult2 = cxd.Encrypt(_DOBAdult2, key);
                            _DOBAdult2 = Convert.ToBase64String(bDOBAdult2);
                        }

                        if (_CountryName.ToString() != "" && _CountryName.ToString() != null)
                        {
                            byte[] bCountryName = cxd.Encrypt(_CountryName, key);
                            _CountryName = Convert.ToBase64String(bCountryName);
                        }

                        if (_AreaName.ToString() != "" && _AreaName.ToString() != null)
                        {
                            byte[] bAreaName = cxd.Encrypt(_AreaName, key);
                            _AreaName = Convert.ToBase64String(bAreaName);
                        }

                        if (_StartDate.ToString() != "" && _StartDate.ToString() != null)
                        {
                            byte[] bStartDate = cxd.Encrypt(_StartDate, key);
                            _StartDate = Convert.ToBase64String(bStartDate);
                        }

                        if (_EndDate.ToString() != "" && _EndDate.ToString() != null)
                        {
                            byte[] bEndDate = cxd.Encrypt(_EndDate, key);
                            _EndDate = Convert.ToBase64String(bEndDate);
                        }

                        if (CTNRIC.ToString() != "" && CTNRIC.ToString() != null)
                        {
                            byte[] bCTNRIC = cxd.Encrypt(CTNRIC, key);
                            CTNRIC = Convert.ToBase64String(bCTNRIC);
                        }

                        if (_Premium.ToString() != "" && _Premium.ToString() != null)
                        {
                            byte[] bPremium = cxd.Encrypt(_Premium, key);
                            _Premium = Convert.ToBase64String(bPremium);
                        }

                        if (_NoofChilds.ToString() != "" && _NoofChilds.ToString() != null)
                        {
                            byte[] bNoofChilds = cxd.Encrypt(_NoofChilds, key);
                            _NoofChilds = Convert.ToBase64String(bNoofChilds);
                        }

                        string _promocode = "COMXPRESS";
                        byte[] bpromocode = cxd.Encrypt(_promocode, key);
                        _promocode = Convert.ToBase64String(bpromocode);

                        if (SaveSelectedInsurance(TAId, _resultId, _companyId, Convert.ToSingle(_AnnualPremium), _UserId, true))
                        {
                            string _url = "";
                            //_durationRange = Get1CoverTravelDuration(_duration);
                            //_url = "http://www.sg.cntaiping.com/UAT/chinainsB2Clib/Travel/SafeQuoteResult.aspx?PROMOCODE=COMXPRESS";
                            //_url = "http://www.sg.cntaiping.com/UAT/chinainsB2Clib/Travel/SafeQuoteResult.aspx?PROMOCODE=" + _promocode;
                            _url = "http://www.sg.cntaiping.com/chinainsB2Clib/Travel/SafeQuoteResult.aspx?PROMOCODE=" + _promocode;
                            _url += "&PRODUCTTYPE=" + _TripType + "&POLICYTYPE=" + _PlanType;
                            _url += "&PLANTYPE=" + _PlanName + "&Name=" + _Name + "&DOB=" + _DOBAdult1 + "&Phone=" + _MobileNum;
                            _url += "&email=" + _Email + "&Name2=" + CTName2 + "&DOB2=" + _DOBAdult2 + "&Destination=" + _CountryName;
                            _url += "&Area=" + _AreaName + "&PolicyStartDate=" + _StartDate + "&PolicyEndDate=" + _EndDate;
                            _url += "&NRIC=" + CTNRIC + "&Premium=" + _Premium + "&CHILDNO=" + _NoofChilds;

                            if (_Mail == true)
                            {
                                if (SaveMailStatus(TAId, _resultId, _companyId, true))
                                {
                                    ProposalNotification(TAId, _companyId, _UserId);
                                }
                            }
                            Response.Redirect(_url.ToString());
                        }
                    }
                    else if (_companyId == 24) //RSA
                    {
                        string _TaAppSeaId = "";
                        string _TripType, _PlanType, _Name, _Email, _DOBAdult1, _DOBAdult2, _MobileNum, _AreaName, _CountryCode;
                        string _StartDate, _EndDate, _Premium, _NoofChilds, _NoofAdults, _PlanName, _Title;

                        _TripType = ""; _PlanType = ""; _Name = ""; _Email = ""; _DOBAdult1 = ""; _DOBAdult2 = ""; _MobileNum = "";
                        _AreaName = ""; _CountryCode = ""; _StartDate = ""; _EndDate = ""; _Premium = ""; _NoofChilds = "";
                        _PlanName = ""; _NoofAdults = ""; _Title = "";

                        if (Request.QueryString["title"] != null)
                        {
                            _Title = Request.QueryString["title"].ToString();
                        }

                        TAQuoteResultDB _taquoteResultdb = new TAQuoteResultDB();
                        DataSet _dsRSA = new DataSet();
                        _dsRSA = _taquoteResultdb.GetTAPlanDetailsRSA(_companyId, _resultId, CountryId, TAId);
                        if (_dsRSA.Tables[0].Rows.Count > 0)
                        {
                            _TaAppSeaId = _dsRSA.Tables[0].Rows[0]["TAAppSeaId"].ToString();
                            _PlanType = _dsRSA.Tables[0].Rows[0]["PlanType"].ToString();
                            _TripType = _dsRSA.Tables[0].Rows[0]["TripType"].ToString();
                            _Name = _dsRSA.Tables[0].Rows[0]["Name"].ToString();
                            _Email = _dsRSA.Tables[0].Rows[0]["EmailAddress"].ToString();
                            _MobileNum = _dsRSA.Tables[0].Rows[0]["MobileNum"].ToString();
                            _DOBAdult1 = _dsRSA.Tables[0].Rows[0]["DOBAdult1"].ToString();
                            _DOBAdult2 = _dsRSA.Tables[0].Rows[0]["DOBAdult2"].ToString();
                            _StartDate = _dsRSA.Tables[0].Rows[0]["StartDate"].ToString();
                            _EndDate = _dsRSA.Tables[0].Rows[0]["EndDate"].ToString();
                            _Premium = string.Format("{0:00.00}", Convert.ToSingle(_dsRSA.Tables[0].Rows[0]["TotalPremium"].ToString()));
                            _NoofChilds = _dsRSA.Tables[0].Rows[0]["NoofChilds"].ToString();
                            _NoofAdults = _dsRSA.Tables[0].Rows[0]["NoofAdults"].ToString();
                            

                            if (_TripType == "S")
                            {
                                _AreaName = _dsRSA.Tables[2].Rows[0]["AreaName"].ToString();
                                _PlanName = _dsRSA.Tables[3].Rows[0]["PlanName"].ToString();

                                _CountryCode = _dsRSA.Tables[1].Rows[0]["CountryCode"].ToString();
                            }
                            else
                            {
                                _AreaName = _dsRSA.Tables[0].Rows[0]["AreaName"].ToString();
                                _PlanName = _dsRSA.Tables[1].Rows[0]["PlanName"].ToString();
                            }
                        }

                        if (SaveSelectedInsurance(TAId, _resultId, _companyId, Convert.ToSingle(_AnnualPremium), _UserId, true))
                        {
                            Int32 promodiscount = GetPromoDiscount(_companyId.ToString());
                            string _origpremium = "0";

                            if (promodiscount > 0)
                            {
                                if (_TripType != "S")
                                {
                                    promodiscount = 20;
                                }

                                _origpremium = string.Format("{0:00.00}", Convert.ToSingle(_AnnualPremium) / (Convert.ToDouble(100 - promodiscount) / 100));
                            }
                            else
                            {
                                _origpremium = _Premium;
                            }
                            string _url = "";                            
                            //_url = "https://RSAgroup.com.sg/rsagrouptest/HomeIns/ASPX/Home.aspx?";
                            //_url = "https://www.rsagroup.com.sg/rsagrouptest/TravelIns/aspx/RSATvlQuoteForm.aspx?";  //testurl
                            _url = "https://www.rsagroup.com.sg/TravelIns/aspx/RSATvlQuoteForm.aspx?";     //liveurl
                            //_url += "chunk=C2531C22-D65B-48DC-801D-4976868BA920";  //testchunk
                            _url += "chunk=5B7B7883-AE86-4F94-811A-AD4836AD0F0A"; //livechunk
                            _url += "&Ctyp=" + _PlanType + "&Ttyp=" + _TripType + "&Cntry=" + _CountryCode;
                            _url += "&Rgn=" + _AreaName + "&StDt=" + _StartDate + "&EndDt=" + _EndDate;
                            _url += "&NoOfAd=" + _NoofAdults + "&NoOfCh=" + _NoofChilds;
                            _url += "&Title=" + _Title + "&FullNam=" + _Name + "&Email=" + _Email;
                            _url += "&Mobile=" + _MobileNum + "&TID=" + _TaAppSeaId + "&Plan=" + _PlanName;
                            _url += "&dob1=" + _DOBAdult1 + "&dob2=" + _DOBAdult2;
                            _url += "&OPrem=" + _origpremium + "&DPrem=" + _Premium;

                            //https://RSAgroup.com.sg/rsagrouptest/HomeIns/ASPX/Home.aspx?chunk=32447C38-2329-4B37-8421-
                            //7E65B173CCD4&Ctyp=I&Ttyp=S&Cntry=AUS&Rgn=&StDt=20/05/2012&EndDt=25/02/2012&NoOfAd=&NoOfCh=&Title=M
                            //&FullNam=Roy&Email=roy.siew@comparexpress.com&Mobile=12345678
                            //&TID=<your unique id for each transaction>&Plan=1

                            if (_Mail == true)
                            {
                                if (SaveMailStatus(TAId, _resultId, _companyId, true))
                                {
                                    ProposalNotification(TAId, _companyId, _UserId);
                                }
                            }
                            Response.Redirect(_url.ToString());
                        }
                    }
                }
            }
            
        }
    }

    private bool SaveSelectedInsurance(Int64 TAAppSeaId, Int64 TAInsId, Int64 ProviderCompanyId, float AnnualPremium, Int64 RefCEUserId, bool Status)
    {
        Int64 _result = 0;
        TASelectedInsurance _taselectedinsurance = new TASelectedInsurance();
        TASelectedInsuranceDB _taselectedinsurancedb = new TASelectedInsuranceDB();
        _taselectedinsurance.TAAppSeaId = TAAppSeaId;
        _taselectedinsurance.TAInsId = TAInsId;
        _taselectedinsurance.ProviderCompanyId = ProviderCompanyId;
        _taselectedinsurance.AnnualPremium = AnnualPremium;
        _taselectedinsurance.RefCEUserId = RefCEUserId;
        _taselectedinsurance.Status = Status;
        _result = _taselectedinsurancedb.Insert(_taselectedinsurance);
        return _result > 0;
    }

    private bool SaveMailStatus(Int64 TAAppSeaId, Int64 TAInsId, Int64 ProviderCompanyId, bool Status)
    {
        Int64 _result = 0;
        TASelectedInsurance _taselectedinsurance = new TASelectedInsurance();
        TASelectedInsuranceDB _taselectedinsurancedb = new TASelectedInsuranceDB();
        _taselectedinsurance.TAAppSeaId = TAAppSeaId;
        _taselectedinsurance.TAInsId = TAInsId;
        _taselectedinsurance.ProviderCompanyId = ProviderCompanyId;
        _taselectedinsurance.Status = Status;
        _result = _taselectedinsurancedb.InsertMailStatus(_taselectedinsurance);
        return _result > 0;
    }

    private void LogUserSelectHistory(string CoverType, Int64 ResultId, Int64 CompanyId)
    {
        UserSelectHistoryDB _userselectdb = new UserSelectHistoryDB();
        if (CoverType.ToLower() == "ind")
        {
            _userselectdb.LogUserSelectHistory("TA", Request.Browser.Type, Request.UserHostAddress, "TAIND", ResultId, CompanyId);
        }
        else if (CoverType.ToLower() == "ins")
        {
            _userselectdb.LogUserSelectHistory("TA", Request.Browser.Type, Request.UserHostAddress, "TAINDSP", ResultId, CompanyId);
        }
        else
        {
            _userselectdb.LogUserSelectHistory("TA", Request.Browser.Type, Request.UserHostAddress, "TAFML", ResultId, CompanyId);
        }

    }

    private void ProposalNotification(Int64 TAAppSeaId, Int64 ProviderCompanyId, Int64 UserId)
    {
        string _date, _time, _refNo, _QuoteDate, _selectedDate, _startDate, _endDate, _providerName, _customerName, _customerEmail, _customerContactNo;
        string _premium, _planType, _planName, _destination, _tripType, _InsuranceName, _CXEmail, _CXCCEmail, _CXBCCEmail, _userId, _NoofAdults, _NoofChilds;

        _date = ""; _time = ""; _refNo = ""; _QuoteDate = ""; _selectedDate = ""; _startDate = ""; _endDate = ""; _providerName = ""; _customerName = "";
        _customerEmail = ""; _customerContactNo = ""; _premium = ""; _planType = ""; _planName = ""; _tripType = ""; _destination = ""; _CXEmail = "";
        _CXCCEmail = ""; _CXBCCEmail = ""; _userId = "0"; _NoofAdults = "0"; _NoofChilds = "0";

        _InsuranceName = "Travel Insurance";
        _CXEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CX_TO");
        _CXCCEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CX_CC");
        _CXBCCEmail = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CX_BCC");

        _userId = UserId.ToString();

        MyDatabase _db = new MyDatabase();
        DataTable _dtResult = new DataTable();
        string _spName = "[sprocTIProposalFromCXSelectItembyProvider]";
        SortedList _sPara = new SortedList();
        _sPara.Add("@TAAppSeaId", TAAppSeaId);
        _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
        _sPara.Add("@RefCEUserId", Convert.ToInt64(_userId));
        _dtResult = _db.GetDataTableBySP(_spName, _sPara);

        if ((_dtResult != null) && (_dtResult.Rows.Count > 0))
        {
            _date = _dtResult.Rows[0]["Date"].ToString();
            _time = _dtResult.Rows[0]["Time"].ToString();
            _refNo = _dtResult.Rows[0]["TAAppSeaId"].ToString();
            _QuoteDate = Convert.ToDateTime(_dtResult.Rows[0]["QuoteDate"].ToString()).ToString("dd/MMM/yyyy");
            _selectedDate = Convert.ToDateTime(_dtResult.Rows[0]["SelectedDate"].ToString()).ToString("dd/MMM/yyyy hh:mm");
            _startDate = Convert.ToDateTime(_dtResult.Rows[0]["StartDate"].ToString()).ToString("dd/MMM/yyyy");
            _endDate = Convert.ToDateTime(_dtResult.Rows[0]["EndDate"].ToString()).ToString("dd/MMM/yyyy");
            _providerName = _dtResult.Rows[0]["CompanyName"].ToString();

            _customerName = _dtResult.Rows[0]["CustomerName"].ToString();
            _customerEmail = _dtResult.Rows[0]["Email"].ToString();
            _customerContactNo = _dtResult.Rows[0]["Mobile"].ToString();

            _premium = _dtResult.Rows[0]["AnnualPremium"].ToString();
            _planType = _dtResult.Rows[0]["PlanType"].ToString();
            _planName = _dtResult.Rows[0]["PlanName"].ToString();
            _tripType = _dtResult.Rows[0]["TripType"].ToString();
            _destination = _dtResult.Rows[0]["Destination"].ToString();
            _NoofAdults = _dtResult.Rows[0]["NoofAdults"].ToString();
            _NoofChilds = _dtResult.Rows[0]["NoofChilds"].ToString();
            MyMailManager.SendTIProposalNotification(_providerName, _InsuranceName, _refNo, _date, _time, _customerName, _customerEmail, _customerContactNo,
                _planName, _planType, _tripType, _destination, _premium, _startDate, _endDate, _QuoteDate, _selectedDate, _CXEmail, _CXCCEmail, _CXBCCEmail,
                _userId, _NoofAdults, _NoofChilds);
        }

    }

    private string Get1CoverTravelDuration(Int32 Duration)
    {
        string _Duration = "0";
        if (Duration >= 1 && Duration <= 3)
        {
            _Duration = "1";
        }
        else if (Duration >= 4 && Duration <= 6)
        {
            _Duration = "2";
        }
        else if (Duration >= 7 && Duration <= 10)
        {
            _Duration = "3";
        }
        else if (Duration >= 11 && Duration <= 14)
        {
            _Duration = "4";
        }
        else if (Duration >= 15 && Duration <= 18)
        {
            _Duration = "5";
        }
        else if (Duration >= 19 && Duration <= 22)
        {
            _Duration = "6";
        }
        else if (Duration >= 23 && Duration <= 27)
        {
            _Duration = "7";
        }
        else if (Duration >= 28 && Duration <= 31)
        {
            _Duration = "8";
        }
        else if (Duration >= 32 && Duration <= 38)
        {
            _Duration = "9";
        }
        else if (Duration >= 39 && Duration <= 45)
        {
            _Duration = "10";
        }
        else if (Duration >= 46 && Duration <= 52)
        {
            _Duration = "11";
        }
        else if (Duration >= 53 && Duration <= 59)
        {
            _Duration = "12";
        }
        else if (Duration >= 60 && Duration <= 66)
        {
            _Duration = "13";
        }
        else if (Duration >= 67 && Duration <= 73)
        {
            _Duration = "14";
        }
        else if (Duration >= 74 && Duration <= 80)
        {
            _Duration = "15";
        }
        else if (Duration >= 81 && Duration <= 87)
        {
            _Duration = "16";
        }
        else if (Duration >= 88 && Duration <= 94)
        {
            _Duration = "17";
        }
        else if (Duration >= 95 && Duration <= 101)
        {
            _Duration = "18";
        }
        else if (Duration >= 102 && Duration <= 108)
        {
            _Duration = "19";
        }
        else if (Duration >= 109 && Duration <= 115)
        {
            _Duration = "20";
        }
        else if (Duration >= 116 && Duration <= 122)
        {
            _Duration = "21";
        }
        else if (Duration >= 123 && Duration <= 129)
        {
            _Duration = "22";
        }
        else if (Duration >= 130 && Duration <= 136)
        {
            _Duration = "23";
        }
        else if (Duration >= 137 && Duration <= 143)
        {
            _Duration = "24";
        }
        else if (Duration >= 144 && Duration <= 150)
        {
            _Duration = "25";
        }
        else if (Duration >= 151 && Duration <= 157)
        {
            _Duration = "26";
        }
        else if (Duration >= 158 && Duration <= 164)
        {
            _Duration = "27";
        }
        else if (Duration >= 165 && Duration <= 171)
        {
            _Duration = "28";
        }
        else if (Duration >= 172 && Duration <= 178)
        {
            _Duration = "29";
        }
        else if (Duration >= 179 && Duration <= 180)
        {
            _Duration = "30";
        }
        return _Duration;
    }

    private Int32 GetPromoDiscount(string providerid)
    {
        Int32 Discount = 0;
        MyDatabase _db = new MyDatabase();
        string strSql = "SELECT ItemType FROM tblCXPromotion WHERE ProviderCompanyId = '" + providerid + "' and PromotionType = 600 and GETDATE() between StartDate and EndDate";
        DataTable _dt = new DataTable("aTable");
        _dt = _db.GetDataTable(strSql);

        if (_dt != null && _dt.Rows.Count > 0)
        {
            Discount = Convert.ToInt32(_dt.Rows[0]["ItemType"].ToString());
        }

        return Discount;

    }

}
